﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;


using Trang;
namespace ArchSoft
{
    public partial class MyFrm : Form
    {
        string FolList = "01 Client";
        string CurDir = "";
        Thread myth = null;

        string PageSetup = "PDF A1";

        public string ListOfFolderNedIss
        {
            get
            {
                return FolList;
            }
        }

        public string ListOfPageSetup
        {
            get
            {
                return PageSetup;
            }
        }

        public void setCurDir(string curDir)
        {
            this.CurDir = curDir;
        }

        public string ProjectFolder
        {
            set { CurDir = value; }
        }

        public MyFrm(bool CopyXrefable)
        {
            InitializeComponent();
            // this.Ra_Xf.Enabled = CopyXrefable;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.Items.Count <= 0)
            {
                //MessageBox.Show("Please choose a page setup", "Issued", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MessageBox.Show("Xin vui lòng chọn một thiết lập trang ", "Phát hành", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (txtDes.Text == "")
            {
                //MessageBox.Show("Please fill in description field", "Issued", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MessageBox.Show("Vui lòng nhập vào phần mô tả", "Phát hành", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            foreach (RadioButton bx in this.groupBox1.Controls)
            {
                if (bx.Checked)
                {
                    FolList = bx.Tag.ToString();
                    //  MessageBox.Show(bx.Tag.ToString());
                    break;
                }
            }

            this.DialogResult = DialogResult.Yes;
            this.Close();

            //foreach (RadioButton bx in this.groupBox3.Controls)
            //{
            //    if (bx.Checked)
            //    {
            //        PageSetup = bx.Tag.ToString();
            //    }
            //}

            //if (this.Chx_DWG.Checked)
            //{
            //    //Issued!
            //    this.DialogResult = DialogResult.OK;
            //    this.Close();
            //}
            //else if (this.Ra_Xf.Checked)
            //{
            //    //Copy Xref!!!
            //    myth = new Thread(new ThreadStart(CopyIn));
            //    myth.IsBackground = true;
            //    ProgressingWin fr = new ProgressingWin(myth);
            //    fr.MessText = "None";
            //    fr.Text = "Issued Xref";
            //    this.Opacity = 0;
            //    fr.ShowDialog();

            //    this.DialogResult = DialogResult.Cancel;
            //    this.Close();
            //}
            //else if ( this.radioButtonPDF.Checked)
            //{
            //    //MessageBox.Show( GetDate());
            //    this.DialogResult = DialogResult.Yes;
            //   // MessageBox.Show(FolList);
            //    this.Close();
            //}
        }

        #region " For  Copy Xref! "

        string GetDate()
        {
            DateTime dateTime = DateTime.Now;
            return dateTime.Year + "." + dateTime.Month.ToString("00") + "." + dateTime.Day.ToString("00");
        }

        private string GetUniqueName(string mainName, string StoreDir)
        {
            int order = 0;
            string result = mainName;

            while (Directory.Exists(StoreDir + @"\" + result))
            {
                order++;
                result = mainName + "(" + order + ")";
            }
            return result;
        }

        void CopyIn()
        {
            try
            {
                foreach (RadioButton bx in this.groupBox1.Controls)
                {
                    if (bx.Checked)
                    {
                        string CpFrom = CurDir + Path.DirectorySeparatorChar + ProjectManager.PROJECT_OUTPUT_FOLDER + "\\Xref";
                        string CpHere = CurDir + Path.DirectorySeparatorChar + ProjectManager.PROJECT_ISSUED_FOLDER + Path.DirectorySeparatorChar + bx.Tag.ToString() + "\\Xref";
                        CpHere = CpHere + Path.DirectorySeparatorChar + GetUniqueName(GetDate(), CpHere);
                        MoveDirectory(CpFrom, CpHere);
                        //if (MessageBox.Show("Issueing folder [" + CpHere + "] completed. Do you want to open it?", "Complete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        if (MessageBox.Show("Phát hành thư mục [" + CpHere + "] hoàn thành. Bạn có muốn mở thư mục?", "Hoàn thành", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            System.Diagnostics.Process.Start(CpHere);
                        break;
                    }
                }//Ket thuc Duyet
            }
            catch { }
        }

        void CopyIn1()
        {
            try
            {
                foreach (RadioButton bx in this.groupBox1.Controls)
                {
                    if (bx.Checked)
                    {
                        string CpFrom = CurDir + Path.DirectorySeparatorChar + ProjectManager.PROJECT_OUTPUT_FOLDER + "\\Xref";
                        string CpHere = CurDir + Path.DirectorySeparatorChar + ProjectManager.PROJECT_ISSUED_FOLDER + Path.DirectorySeparatorChar + bx.Tag.ToString() + "\\PDF";
                        CpHere = CpHere + Path.DirectorySeparatorChar + GetUniqueName(GetDate(), CpHere);
                        MoveDirectory(CpFrom, CpHere);
                        //if (MessageBox.Show("Issuei folder [" + CpHere + "] completed. Do you want to open it?", "Complete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        if (MessageBox.Show("Phát hành thư mục [" + CpHere + "] hoàn thành. Bạn có muốn mở thư mục?", "Hoàn thành", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            System.Diagnostics.Process.Start(CpHere);
                        break;
                    }
                }//Ket thuc Duyet
            }
            catch { }
        }

        void MoveDirectory(string strSourceDir, string strDestDir)
        {
            if (Directory.Exists(strSourceDir))
            {
                try
                {
                    CopyDirectory(new DirectoryInfo(strSourceDir), new DirectoryInfo(strDestDir));
                }
                catch (Exception subEx)
                {
                    throw subEx;
                }
            }
        }

        void CopyDirectory(DirectoryInfo diSourceDir, DirectoryInfo diDestDir)
        {
            if (!diDestDir.Exists) diDestDir.Create();
            FileInfo[] fiSrcFiles = diSourceDir.GetFiles();
            foreach (FileInfo fiSrcFile in fiSrcFiles)
            {
                fiSrcFile.CopyTo(Path.Combine(diDestDir.FullName, fiSrcFile.Name));
            }
            DirectoryInfo[] diSrcDirectories = diSourceDir.GetDirectories();
            foreach (DirectoryInfo diSrcDirectory in diSrcDirectories)
            {
                CopyDirectory(diSrcDirectory, new DirectoryInfo(Path.Combine(diDestDir.FullName, diSrcDirectory.Name)));
            }
        }

        #endregion

        private void buttonReload_Click(object sender, EventArgs e)
        {
            try
            {
                //if (!KiemTraForFull("reloadToolStripMenuItem"))
                //    return;
                //if (!AutoCadChecking())
                //{
                //    //MessageBox.Show("This function need AutoCAD environment to run. Please install or re-check AutoCAD program.", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}




                string newfl = CurDir;

                if (newfl == "")
                {
                    //MessageBox.Show("Basic files in current project may be lost.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    MessageBox.Show("Tập tin cơ bản trong dự án hiện tại có thể bị mất.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                newfl += Path.DirectorySeparatorChar + ProjectManager.PROJECT_OUTPUT_FOLDER + "\\Xref\\" + ProjectManager.DEFAULT_TEMPLATE_FILE;

                if (!File.Exists(newfl))
                {
                    //if (MessageBox.Show("Neccessary file in current project may be lost. Do you want to create a new file?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    if (MessageBox.Show("Tập tin cần thiết trong dự án hiện tại có thể bị mất. Bạn có muốn tạo tập tin mới?", "Lỗi", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        string sorfl = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Trang\\" + ProjectManager.DEFAULT_APP_NAME + Path.DirectorySeparatorChar + ProjectManager.DEFAULT_TEMPLATE_FILE; //A_Template.dwt
                        File.Copy(sorfl, newfl);
                    }
                    else
                        return;
                }


                string templateFile = newfl;
                if (templateFile != string.Empty)
                    AutocadHelper.Instance.TemplateFileForPublishing = templateFile;
                if (AutocadHelper.Instance.TemplateFileForPublishing != string.Empty)
                {
                    PutLayoutComboBoxItems();
                }



            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }


        private void PutLayoutComboBoxItems()
        {
            try
            {

                this.Cursor = Cursors.WaitCursor;

                //Application.DoEvents();
                // Thread.Sleep(100);

                List<string> pageList = AutocadHelper.Instance.PageSetupList;



                foreach (string item in pageList)
                {
                    if (item.IndexOf("PDF A", StringComparison.CurrentCultureIgnoreCase) >= 0)
                        comboBox1.Items.Add(item);
                }

                this.Cursor = Cursors.Default;
                //MessageBox.Show("Complete!", "Reload Page Setup");
                MessageBox.Show("Hoàn thành!", "Tải lại trang");

                comboBox1.SelectedIndex = 0;
                comboBox1.Focus();

            }
            catch (Exception ex)
            {
                //this.Cursor = Cursors.Default;
                //MessageBox.Show(ex.Message + "\n Please re-check your AutoCAD program.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(ex.Message + "\n Vui lòng kiểm tra lại chương trình AutoCAD của bạn.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            PageSetup = comboBox1.SelectedItem.ToString();
        }

        private void MyFrm_Load(object sender, EventArgs e)
        {

        }
    }
}